package frog.lazy.framework.http.advice;

import cn.hutool.json.JSONUtil;
import frog.lazy.framework.core.dto.ResultDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;


/**
 * 对返回统一处理
 * @author lwy frog.lazy.manager.api.controller.biz
 */
@Slf4j
@ControllerAdvice(annotations = RestController.class)
public class HttpResultAdvice implements ResponseBodyAdvice {
    @Override
    public boolean supports(MethodParameter methodParameter, Class aClass) {
        return true;
    }

    @Override
    public Object beforeBodyWrite(Object o, MethodParameter methodParameter, MediaType mediaType, Class aClass, ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse) {
        if(o instanceof ResultDto){
            return o;
        }
        if(o instanceof String){
            return o;
        }
        return ResultDto.success(o);
    }

}
